package com.wx.studentManage.util;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

@WebServlet(value = "/code")
public class Code extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 创建空白图片
        BufferedImage image = new BufferedImage(100,30,BufferedImage.TYPE_INT_RGB);
        // 1.获取图片画笔
        Graphics g = image.getGraphics();
        Random r = new Random();// 随机数
        // 2.设置画笔颜色
        // 设置背景颜色，RGB 三个参数
        g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
        // 3.绘制矩形背景
        g.fillRect(0,0,100,30);

        // 4.调用自定义方法，获取长度为4的字母与数字组合的字符串
        String num = getNumber(4);
        System.out.println(num);
        req.getSession().setAttribute("checkCode",num);
        g.setColor(new Color(0,0,0));
        g.setFont(new Font("宋体",Font.BOLD,24));
        // 5.设置字体颜色后，绘制字符串
        g.drawString(num,25,25);
        // 6.绘制8条干扰线
        for (int i=0;i<8;i++){
            g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
            g.drawLine(r.nextInt(100),
                    r.nextInt(30),
                    r.nextInt(100),
                    r.nextInt(30));
        }
        resp.setContentType("image/ jpeg");
        OutputStream ops = resp.getOutputStream();
        ImageIO.write(image,"jpeg", ops);
        ops.close();
    }

    /**
     * 字符串的拼接
     * @param
     * @return
     */
    private String getNumber(int size) {
        String str = "ASDFGHJKLQWERTYUIPZXCVBNMasdfghjkqwertyuipzxcvbnm23456789";
        StringBuilder number = new StringBuilder();
        Random r = new Random() ;
        for(int i=0;i<size;i++) {
            number.append(str.charAt(r.nextInt(str.length()))) ;
//            str.charAt 拿到下标为0-str长度之间的下标，下h标从0开始
        }
            return number.toString();
        }
}
